package com.example.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import java.util.Locale;

public class LocaleManager {
    
    private static final String PREFS_NAME = "locale_prefs";
    private static final String KEY_LANGUAGE = "language";
    private static final String KEY_COUNTRY = "country";
    
    // 支持的语言
    public static final String LANGUAGE_CHINESE = "zh";
    public static final String LANGUAGE_ENGLISH = "en";
    
    // 支持的国家/地区
    public static final String COUNTRY_CHINA = "CN";
    public static final String COUNTRY_US = "US";
    
    // 语言选项
    public static class LanguageOption {
        public String language;
        public String country;
        public String displayName;
        
        public LanguageOption(String language, String country, String displayName) {
            this.language = language;
            this.country = country;
            this.displayName = displayName;
        }
        
        public Locale getLocale() {
            return new Locale(language, country);
        }
        
        @Override
        public String toString() {
            return displayName;
        }
    }
    
    // 默认支持的语言列表
    public static final LanguageOption[] SUPPORTED_LANGUAGES = {
        new LanguageOption(LANGUAGE_CHINESE, COUNTRY_CHINA, "中文"),
        new LanguageOption(LANGUAGE_ENGLISH, COUNTRY_US, "English")
    };
    
    /**
     * 设置应用语言
     */
    public static Context setLocale(Context context, String language, String country) {
        saveLanguage(context, language, country);
        return updateResources(context, language, country);
    }
    
    /**
     * 设置应用语言（使用LanguageOption）
     */
    public static Context setLocale(Context context, LanguageOption option) {
        return setLocale(context, option.language, option.country);
    }
    
    /**
     * 获取保存的语言设置
     */
    public static LanguageOption getSavedLanguage(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String language = prefs.getString(KEY_LANGUAGE, getSystemLanguage());
        String country = prefs.getString(KEY_COUNTRY, getSystemCountry());
        
        // 查找匹配的语言选项
        for (LanguageOption option : SUPPORTED_LANGUAGES) {
            if (option.language.equals(language) && option.country.equals(country)) {
                return option;
            }
        }
        
        // 如果没有找到匹配的，返回默认（中文）
        return SUPPORTED_LANGUAGES[0];
    }
    
    /**
     * 保存语言设置
     */
    private static void saveLanguage(Context context, String language, String country) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        prefs.edit()
                .putString(KEY_LANGUAGE, language)
                .putString(KEY_COUNTRY, country)
                .apply();
        
        LogUtil.d("LocaleManager", "Language saved: " + language + "_" + country);
    }
    
    /**
     * 更新应用资源
     */
    private static Context updateResources(Context context, String language, String country) {
        Locale locale = new Locale(language, country);
        Locale.setDefault(locale);
        
        Resources resources = context.getResources();
        Configuration configuration = new Configuration(resources.getConfiguration());
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale);
            context = context.createConfigurationContext(configuration);
        } else {
            configuration.locale = locale;
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }
        
        LogUtil.d("LocaleManager", "Resources updated for locale: " + locale.toString());
        return context;
    }
    
    /**
     * 获取系统默认语言
     */
    private static String getSystemLanguage() {
        return Locale.getDefault().getLanguage();
    }
    
    /**
     * 获取系统默认国家
     */
    private static String getSystemCountry() {
        return Locale.getDefault().getCountry();
    }
    
    /**
     * 检查是否是中文
     */
    public static boolean isChineseLocale(Context context) {
        LanguageOption savedLanguage = getSavedLanguage(context);
        return LANGUAGE_CHINESE.equals(savedLanguage.language);
    }
    
    /**
     * 检查是否是英文
     */
    public static boolean isEnglishLocale(Context context) {
        LanguageOption savedLanguage = getSavedLanguage(context);
        return LANGUAGE_ENGLISH.equals(savedLanguage.language);
    }
    
    /**
     * 获取当前语言的显示名称
     */
    public static String getCurrentLanguageDisplayName(Context context) {
        return getSavedLanguage(context).displayName;
    }
    
    /**
     * 应用保存的语言设置
     */
    public static Context applySavedLanguage(Context context) {
        LanguageOption savedLanguage = getSavedLanguage(context);
        return updateResources(context, savedLanguage.language, savedLanguage.country);
    }
}